var up_doc = window.dialogArguments.document;
function $(name) {
	return document.getElementsByName(name);
}

function $2(name) {
	return window.dialogArguments.document.getElementsByName(name);
}

var fieldNameMap = {
	address : "my:企业地址",
	linkman : "my:联系人",
	supplyCycle : "my:供货周期",
	supplyAddress : "my:发货地址",
	tel : "my:电话号码",
	cpurbillcode : "my:发票税码",
	taxRegister : "my:税务登记号",
	fax : "my:传真号码",
	couttype : ["my:物流方式", "my:物流方式PK"],
	qualitySystem : "my:质量体系",
	postCode : "my:邮编",
	companyType : ["my:企业类型", "my:企业类型PK"],
	ccuspaycond : "my:付款条件",
	property : "my:属性DF",
	email : "my:电子邮箱",
	ccontent : "my:价格条款",
	fax : "my:传真号码",
	cvenbank : "my:银行类别",
	bank : "my:开户银行",
	bankaccountname : "my:银行开户名",
	bankcode : "my:银行代码",
	currency : ["my:币种", "my:币种PK"],
	bankaccount : "my:银行账号",
	providerName : "my:供应商名称",
	providerCode : "my:供应商编码"
};
var ccontentValueArray = ["出厂价", "到库价", "FOB", "CIF", "C&F", "其他"];
var ccuspaycondValueArray = ["票后30天", "票后45天", "票后60天", "票后70天", "其他"];
function isExistInArray(array, value) {
	for (var i in array) {
		if (array[i] == value) {
			return i;
		}
	}
	return -1;
}
function setCcontentAndCcuspaycond() {
	var ccontentValue = document.getElementsByName('ccontent')[0].value;
	var ccuspaycondValue = document.getElementsByName('ccuspaycond')[0].value;
	var ccontentIndex = (ccontentValue == '' ? 0 : isExistInArray(
			ccontentValueArray, ccontentValue));
	var ccuspaycondIndex = (ccuspaycondValue == '' ? 0 : isExistInArray(
			ccuspaycondValueArray, ccuspaycondValue));

	if (ccontentValue == '') {
		document.getElementsByName('ccontent')[0].value = ccontentValueArray[0];
	}

	if (ccuspaycondValue == '') {
		document.getElementsByName('ccuspaycond')[0].value = ccuspaycondValueArray[0];
	}

	if (ccontentIndex == -1) {
		ccontentIndex = 5;
		document.getElementById("ccontentDiv").style.display = '';
	} else {
		document.getElementById("ccontentDiv").style.display = 'none';
	}

	if (ccuspaycondIndex == -1) {
		ccuspaycondIndex = 4;
		document.getElementById("ccuspaycondDiv").style.display = '';
	} else {
		document.getElementById("ccuspaycondDiv").style.display = 'none';
	}
	document.getElementsByName("ccontentSelect")[0].value = ccontentIndex;
	document.getElementsByName("ccuspaycondSelect")[0].value = ccuspaycondIndex;
}
function _ajax(url, operator, isPost) {
	window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
			- 3 + "px";
	window.parent.document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				var data = eval("(" + text + ")");
				if (data.success)
					operator(data);
				else
					alert(data.data);
			} else {
				alert("连接服务器异常");
			}
			window.parent.document.getElementById("lodingDiv").style.display = 'none';
		}
	};
	xmlhttp.open(isPost ? "post" : "get", url + "&temp=" + Math.random(), true);
	xmlhttp.send();
}

function onCcontentSelectChange(node) {
	if (node.value == 5) {
		document.getElementById("ccontentDiv").style.display = '';
		document.getElementsByName('ccontent')[0].value = '';
	} else {
		document.getElementById("ccontentDiv").style.display = 'none';
		document.getElementsByName('ccontent')[0].value = ccontentValueArray[node.value];
	}
}

function onCcuspaycondSelectChange(node) {
	if (node.value == 5) {
		document.getElementById("ccuspaycondDiv").style.display = '';
		document.getElementsByName('ccuspaycond')[0].value = '';
	} else {
		document.getElementById("ccuspaycondDiv").style.display = 'none';
		document.getElementsByName('ccuspaycond')[0].value = ccuspaycondValueArray[node.value];
	}
}

function queryProviderInfo() {
	var providerCode = $("query_providerCode")[0].value.replace(/ /g, '')
			.toUpperCase();

	if (providerCode == "") {
		alert("供应商编码不能为空");
		return;
	}

	_ajax(	"./oa2uapController.do?method=findProviderInfoMPI&providerCode="
					+ providerCode + "&corp=" + $2("my:单位选择器")[0]['default'],
			function(data) {
				var info = eval('(' + data.data + ')')
				for (var key in fieldNameMap) {
					$(key)[0].value = info[key] ? info[key] : "";
					if (key != "providerName" && key != "providerCode") {
						$(key)[0].disabled = false;
						$(key)[0].className = "manualInput";
					}
				}
				$("ccuspaycondSelect")[0].disabled = false;
				$("ccuspaycondSelect")[0].className = "manualInput";

				$("ccontentSelect")[0].disabled = false;
				$("ccontentSelect")[0].className = "manualInput";
				setCcontentAndCcuspaycond();
			});
}

function writeToForm() {
	for (var key in fieldNameMap) {
		try {
			if (typeof fieldNameMap[key] == 'string') {
				$2(fieldNameMap[key])[0].value = $(key)[0].value;
			} else {
				var node = $(key)[0];
				$2(fieldNameMap[key][0])[0].value = node.options[node.selectedIndex].text;
				$2(fieldNameMap[key][1])[0].value = node.value;
			}
		} catch (e) {
			alert(key);
		}
	}
	window.close();
}

window.onload = function() {
	if ($2("my:供应商编码")[0].value != "") {
		for (var key in fieldNameMap) {
			$(key)[0].value = (typeof fieldNameMap[key] == 'string')
					? $2(fieldNameMap[key])[0].value
					: $2(fieldNameMap[key][1])[0].value;
			if (key != "providerName" && key != "providerCode") {
				$(key)[0].disabled = false;
				$(key)[0].className = "manualInput";
			}
		}
		$("ccuspaycondSelect")[0].disabled = false;
		$("ccuspaycondSelect")[0].className = "manualInput";

		$("ccontentSelect")[0].disabled = false;
		$("ccontentSelect")[0].className = "manualInput";
		setCcontentAndCcuspaycond();
	}
}